package com.hjm.app.fragment.user;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.hjm.java.entity.UserLogin;
import com.hjm.app.R;

public class FragmentUserLogin extends Fragment implements View.OnClickListener {
    private static final String TAG = "FragmentUserLogin";
    private OnClickListener onClickListener;
    private EditText etUsername, etPassword;
    private Button btnLogin, btnRegister;
    private UserLogin userLogin;
    public FragmentUserLogin(OnClickListener onClickListener) {
        // Required empty public constructor
        this.onClickListener = onClickListener;
    }

    public static FragmentUserLogin newInstance(String param1, String param2,OnClickListener onClickListener) {
        FragmentUserLogin fragment = new FragmentUserLogin(onClickListener);
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (userLogin != null){
            etUsername.setText(userLogin.getLoginName());
            etPassword.setText(userLogin.getPassword());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_user_login, container, false);
        etUsername = view.findViewById(R.id.edit_text_user_login_phone);
        etPassword = view.findViewById(R.id.edit_text_user_login_password);
        btnLogin = view.findViewById(R.id.button_user_login_login);
        btnRegister = view.findViewById(R.id.button_user_login_register);

        btnLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);

//        etUsername.setText("13700002703");
//        etPassword.setText("123456");
        return view;
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.button_user_login_login){
            onClickListener.onLoginClick(etUsername.getText().toString(), etPassword.getText().toString());
        } else if (v.getId() == R.id.button_user_login_register){
            onClickListener.onRegisterClick();
        }
    }

    public interface OnClickListener {
        void onLoginClick(String username, String password);

        void onRegisterClick();
    }
    public void setUserLogin(String userName, String password){
        this.userLogin = new UserLogin(userName, password);
    }
}